package main

import (
	"fmt"
	"strings"
)

// Go 允许在没有值可供发送的情况下通过 close 函数关闭通道
func main() {
	downstream := make(chan string)
	upstream := make(chan string)
	go sourceGopher(downstream)
	go filterGopher(downstream, upstream)
	printGopher(upstream)
}
func sourceGopher(downstream chan string) {
	for _, v := range []string{"hello world", "a bad apple", "goodbye all"} {
		downstream <- v
	}
	close(downstream)
}
func filterGopher(upstream, downstream chan string) {
	for {
		// 执行以下代码可得知通道是否被关闭
		item, ok := <-upstream
		if !ok {
			close(downstream)
			return
		}
		if !strings.Contains(item, "bad") {
			downstream <- item
		}
	}
}
func printGopher(upstream chan string) {
	for v := range upstream {
		fmt.Println(v)
	}
}
